package com.yuanzi.lang;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class JsonObject extends JSONObject {

	private static final long serialVersionUID = -1455485524647978805L;

	/**
	 * 存放数值型，默认值0
	 * @param key
	 * @param value
	 * @return
	 */
	public Object putNumber(String key, Object value) {
		if(value == null) {
			value = 0;
		}
		return super.put(key, value);
	}
	
	/**
	 * 存放字符串型，默认值""
	 * @param key
	 * @param value
	 * @return
	 */
	public Object putString(String key, Object value) {
		if(value == null) {
			value = "";
		}
		return super.put(key, value);
	}
	
	/**
	 * 存放长整型，转为字符串，默认值""
	 * @param key
	 * @param value
	 * @return
	 */
	public Object putLong(String key, Object value) {
		if(value != null) {
			String str = String.valueOf(value);
			return super.put(key, str);
		}
		return super.put(key, "");
	}
	
	/**
	 * 存放数组，默认值[]
	 * @param key
	 * @param value
	 * @return
	 */
	public Object putArray(String key, Object value) {
		if(value == null) {
			value = new JSONArray();
		}
		return super.put(key, value);
	}
	
	public String toJSONString() {
		return JSON.toJSONString(SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty, 
				SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.DisableCircularReferenceDetect);
	}
	
}
